package com.se.frontline.managers;

import java.io.IOException;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.color.Color;
import org.andengine.util.debug.Debug;

import com.se.frontline.FrontlineSmoothCamera;
import com.se.frontline.MainActivity;
import com.se.frontline.gameplay.GameBackground;
import com.se.frontline.gameplay.GameGround;
import com.se.frontline.gameplay.levels.Level;
import com.se.frontline.multiplayer.MultiplayerExtensionExample;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;

public class ResourceManager extends Object {

	// ====================================================
	// CONSTANTS
	// ====================================================
	private static final ResourceManager INSTANCE = new ResourceManager();

	// ====================================================
	// VARIABLES
	// ====================================================
	// We include these objects in the resource manager for
	// easy accessibility across our project.
	public Engine engine;
	public Context context;
	public MainActivity activity;
	public MultiplayerExtensionExample activity2;
	public GameGround gameground;
	public GameBackground gamebackground;
	public float cameraWidth;
	public float cameraHeight;
	public float cameraScaleFactorX;
	public float cameraScaleFactorY;

	// ====================================================
	// BEGIN OF OUR RESOURCE VARIABLES
	// ====================================================
	// The resource variables listed should be kept public, allowing us easy
	// access
	// to them when creating new Sprite and Text objects and to play sound
	// files.

	// ======================== Background Resources =========== //

	public ITextureRegion mBackgroundLeftTextureRegion;
	public ITextureRegion mBackgroundRightTextureRegion;

	// ======================== Menu Resources ================= //

	public static ITextureRegion gameBackgroundTextureRegion;

	// ======================== Game Resources ================= //

	public ITiledTextureRegion mHero1TextureRegion;
	public ITiledTextureRegion mUnit1TextureRegion;
	public ITiledTextureRegion mUnit2TextureRegion;
	public ITiledTextureRegion mUnit3TextureRegion;
	public ITiledTextureRegion mUnit4TextureRegion;
	public ITiledTextureRegion mUnit5TextureRegion;
	public ITiledTextureRegion mUnit6TextureRegion;
	public ITiledTextureRegion towerTextureRegion;
	public ITiledTextureRegion mUnit1EnemyTextureRegion;

	// ======================== HUD Resources ================== //

	public ITextureRegion JoystickBaseTextureRegion;
	public ITextureRegion JoystickKnobTextureRegion;

	public ITiledTextureRegion fightHUDTextureRegion;
	public ITiledTextureRegion pauseHUDTextureRegion;
	public ITiledTextureRegion playHUDTextureRegion;
	public ITiledTextureRegion menuTextureRegion;
	public ITiledTextureRegion finishedTextureRegion;
	public ITiledTextureRegion optionsHUDTextureRegion;
	public ITiledTextureRegion multiplayerHUDTextureRegion;
	public ITiledTextureRegion restartHUDTextureRegion;
	public ITiledTextureRegion endHUDTextureRegion;
	public ITiledTextureRegion starTextureRegion;

	public ITiledTextureRegion mUnit1HUDTextureRegion;
	public ITiledTextureRegion mUnit2HUDTextureRegion;
	public ITiledTextureRegion mUnit3HUDTextureRegion;
	public ITiledTextureRegion mUnit4HUDTextureRegion;
	public ITiledTextureRegion mUnit5HUDTextureRegion;
	public ITiledTextureRegion mUnit6HUDTextureRegion;
	// ====================================================
	// END OF OUR RESOURCE VARIABLES
	// ====================================================

	// This variable will be used to revert the TextureFactory's default path
	// when we change it.
	private String mPreviousAssetBasePath = "";

	// ====================================================
	// CONSTRUCTOR
	// ====================================================
	private ResourceManager() {
	}

	// ====================================================
	// GETTERS & SETTERS
	// ====================================================
	// Retrieves a global instance of the ResourceManager
	public static ResourceManager getInstance() {
		return INSTANCE;
	}

	public Engine getEngine() {
		return getInstance().engine;
	}

	public Context getContext() {
		return getInstance().context;
	}

	public MainActivity getActivity() {
		return getInstance().activity;
	}

	public GameGround getGameGround() {
		return getInstance().gameground;
	}

	public void setGameGround(GameGround pGameGround) {
		gameground = pGameGround;
	}

	public GameBackground getGameBackground() {
		return getInstance().gamebackground;
	}

	public void setGameBackground(GameBackground pGameBackground) {
		gamebackground = pGameBackground;
	}

	public FrontlineSmoothCamera getCamera() {
		return (FrontlineSmoothCamera) getInstance().engine.getCamera();
	}

	// ====================================================
	// SETUP METHOD
	// ====================================================
	public void setup(final MainActivity pActivity, final Engine pEngine,
			final Context pContext, final float pCameraWidth,
			final float pCameraHeight, final float pCameraScaleX,
			final float pCameraScaleY) {
		activity = pActivity;
		engine = pEngine;
		context = pContext;
		cameraWidth = pCameraWidth;
		cameraHeight = pCameraHeight;
		cameraScaleFactorX = pCameraScaleX;
		cameraScaleFactorY = pCameraScaleY;
	}
	
/*	public void setup2(final MultiplayerExtensionExample pActivity, final Engine pEngine,
			final Context pContext, final float pCameraWidth,
			final float pCameraHeight, final float pCameraScaleX,
			final float pCameraScaleY) {
		activity2 = pActivity;
		engine = pEngine;
		context = pContext;
		cameraWidth = pCameraWidth;
		cameraHeight = pCameraHeight;
		cameraScaleFactorX = pCameraScaleX;
		cameraScaleFactorY = pCameraScaleY;
	}*/

	// ====================================================
	// LOAD BACKGROUND TEXTURES
	// ====================================================
	public void loadBackgroundTextures(Level CurrentLevel) {
		// Store the current asset base path to apply it after we've loaded our
		// textures
		mPreviousAssetBasePath = BitmapTextureAtlasTextureRegionFactory
				.getAssetBasePath();
		// Set our game assets folder to "assets/gfx/game/background/"
		BitmapTextureAtlasTextureRegionFactory
				.setAssetBasePath("gfx/game/background/");

		// Create the background left texture atlas
		BuildableBitmapTextureAtlas backgroundTextureLeft = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 800, 480, TextureOptions.BILINEAR);

		// Create the background left texture region
		mBackgroundLeftTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(backgroundTextureLeft, context,
						"Unbenannt2.png");

		// Build and load the background left texture atlas
		try {
			backgroundTextureLeft
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			backgroundTextureLeft.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// Create the background right texture atlas
		BuildableBitmapTextureAtlas backgroundTextureRight = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 800, 480, TextureOptions.BILINEAR);

		// Create the background right texture region
		mBackgroundRightTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(backgroundTextureRight, context,
						"Unbenannt.png");

		// Build and load the background right texture atlas
		try {
			backgroundTextureRight
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		backgroundTextureRight.load();

		// Revert the Asset Path.
		BitmapTextureAtlasTextureRegionFactory
				.setAssetBasePath(mPreviousAssetBasePath);
	}

	// ============================ UNLOAD BACKGROUND TEXTURES ===============
	// //
	public void unloadBackgroundTextures() {
		// background texture - only unload it if it is loaded:
		if (mBackgroundRightTextureRegion != null) {
			if (mBackgroundRightTextureRegion.getTexture().isLoadedToHardware()) {
				mBackgroundRightTextureRegion.getTexture().unload();
				mBackgroundRightTextureRegion = null;
			}
		}
		if (mBackgroundLeftTextureRegion != null) {
			if (mBackgroundLeftTextureRegion.getTexture().isLoadedToHardware()) {
				mBackgroundLeftTextureRegion.getTexture().unload();
				mBackgroundLeftTextureRegion = null;
			}
		}
	}

	// ====================================================
	// LOAD HUD TEXTURES
	// ====================================================
	public void loadHUDTextures() {
		// Store the current asset base path to apply it after we've loaded our
		// textures
		mPreviousAssetBasePath = BitmapTextureAtlasTextureRegionFactory
				.getAssetBasePath();
		// Set our game assets folder to "assets/gfx/HUD/"
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/HUD/");

		// ============================ Joystick Base Resource ===============
		// //
		BuildableBitmapTextureAtlas JoystickBaseTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 200, 200, TextureOptions.BILINEAR);
		JoystickBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(JoystickBaseTexture, context, "base.png");
		try {
			JoystickBaseTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			JoystickBaseTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Joystick Knob Resource ===============
		// //
		BuildableBitmapTextureAtlas JoystickKnobTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 128, TextureOptions.BILINEAR);
		JoystickKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(JoystickKnobTexture, context, "knob.png");
		try {
			JoystickKnobTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			JoystickKnobTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Pause Button Resource ===============

		BuildableBitmapTextureAtlas pauseHUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
		pauseHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(pauseHUDTexture, context, "pause.png", 1,
						1);
		try {
			pauseHUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			pauseHUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		// ============================ Fight Button Resource ===============

				BuildableBitmapTextureAtlas fightHUDTexture = new BuildableBitmapTextureAtlas(
						engine.getTextureManager(), 100, 100, TextureOptions.BILINEAR);
				fightHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
						.createTiledFromAsset(fightHUDTexture, context, "fightButton.png", 1,
								1);
				try {
					fightHUDTexture
							.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
									0, 0, 0));
					fightHUDTexture.load();
				} catch (TextureAtlasBuilderException e) {
					e.printStackTrace();
				}

		// ============================ Play Button Resource ===============

		BuildableBitmapTextureAtlas playHUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
		playHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(playHUDTexture, context, "play.png", 1, 1);
		try {
			playHUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			playHUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		// ============================ Menu Resource ===============

				BuildableBitmapTextureAtlas menuTexture = new BuildableBitmapTextureAtlas(
						engine.getTextureManager(), 800, 480, TextureOptions.BILINEAR);
				menuTextureRegion = BitmapTextureAtlasTextureRegionFactory
						.createTiledFromAsset(menuTexture, context, "Menu.png", 1, 1);
				try {
					menuTexture
							.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
									0, 0, 0));
					menuTexture.load();
				} catch (TextureAtlasBuilderException e) {
					e.printStackTrace();
				}
				
				// ============================ Finished  Resource ===============

				BuildableBitmapTextureAtlas finishedTexture = new BuildableBitmapTextureAtlas(
						engine.getTextureManager(), 550, 180, TextureOptions.BILINEAR);
				finishedTextureRegion = BitmapTextureAtlasTextureRegionFactory
						.createTiledFromAsset(finishedTexture, context, "finished.png", 1, 1);
				try {
					finishedTexture
							.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
									0, 0, 0));
					finishedTexture.load();
				} catch (TextureAtlasBuilderException e) {
					e.printStackTrace();
				}

		// ============================ Options Button Resource ===============

		BuildableBitmapTextureAtlas optionsHUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 100, 100, TextureOptions.BILINEAR);
		optionsHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(optionsHUDTexture, context,
						"options.png", 1, 1);
		try {
			optionsHUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			optionsHUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		// ============================ Multiplayer Button Resource ===============

				BuildableBitmapTextureAtlas multiplayerHUDTexture = new BuildableBitmapTextureAtlas(
						engine.getTextureManager(), 100, 100, TextureOptions.BILINEAR);
				multiplayerHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
						.createTiledFromAsset(multiplayerHUDTexture, context,
								"multiplayer.png", 1, 1);
				try {
					multiplayerHUDTexture
							.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
									0, 0, 0));
					multiplayerHUDTexture.load();
				} catch (TextureAtlasBuilderException e) {
					e.printStackTrace();
				}

		// ============================ Restart Button Resource ===============

		BuildableBitmapTextureAtlas restartHUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 100, 100, TextureOptions.BILINEAR);
		restartHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(restartHUDTexture, context,
						"restart.png", 1, 1);
		try {
			restartHUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			restartHUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ End Button Resource ===============

		BuildableBitmapTextureAtlas endHUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 100, 100, TextureOptions.BILINEAR);
		endHUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(endHUDTexture, context,
						"end.png", 1, 1);
		try {
			endHUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			endHUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		// ============================ Star Resource ===============

				BuildableBitmapTextureAtlas starTexture = new BuildableBitmapTextureAtlas(
						engine.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
				starTextureRegion = BitmapTextureAtlasTextureRegionFactory
						.createTiledFromAsset(starTexture, context,
								"star.png", 1, 1);
				try {
					starTexture
							.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
									0, 0, 0));
					starTexture.load();
				} catch (TextureAtlasBuilderException e) {
					e.printStackTrace();
				}
		
		
		// ============================ Unit1 Selection Icon Resource
		// =============== //
		BuildableBitmapTextureAtlas unit1HUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 256, TextureOptions.BILINEAR);
		mUnit1HUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(unit1HUDTexture, context, "Unit1.png", 1,
						2);
		try {
			unit1HUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			unit1HUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Unit2 Selection Icon Resource
		// =============== //
		BuildableBitmapTextureAtlas Unit2HUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 256, TextureOptions.BILINEAR);
		mUnit2HUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(Unit2HUDTexture, context, "Unit2.png", 1,
						2);
		try {
			Unit2HUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			Unit2HUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Unit3 Selection Icon Resource
		// =============== //
		BuildableBitmapTextureAtlas Unit3HUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 256, TextureOptions.BILINEAR);
		mUnit3HUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(Unit3HUDTexture, context, "Unit3.png", 1,
						2);
		try {
			Unit3HUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			Unit3HUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Unit4 Selection Icon Resource
		// =============== //
		BuildableBitmapTextureAtlas Unit4HUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 256, TextureOptions.BILINEAR);
		mUnit4HUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(Unit4HUDTexture, context, "Unit4.png", 1,
						2);
		try {
			Unit4HUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			Unit4HUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Unit5 Selection Icon Resource
		// =============== //
		BuildableBitmapTextureAtlas Unit5HUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 256, TextureOptions.BILINEAR);
		mUnit5HUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(Unit5HUDTexture, context, "Unit5.png", 1,
						2);
		try {
			Unit5HUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			Unit5HUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// ============================ Unit6 Selection Icon Resource
		// =============== //
		BuildableBitmapTextureAtlas Unit6HUDTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 128, 256, TextureOptions.BILINEAR);
		mUnit6HUDTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(Unit6HUDTexture, context, "Unit6.png", 1,
						2);
		try {
			Unit6HUDTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			Unit6HUDTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// Revert the Asset Path.
		BitmapTextureAtlasTextureRegionFactory
				.setAssetBasePath(mPreviousAssetBasePath);
	}

	// ============================ UNLOAD HUD TEXTURES =============== //
	public void unloadHUDTextures() {
		// unload it if it is loaded:
		if (JoystickBaseTextureRegion != null) {
			if (JoystickBaseTextureRegion.getTexture().isLoadedToHardware()) {
				JoystickBaseTextureRegion.getTexture().unload();
				JoystickBaseTextureRegion = null;
			}
		}
		if (JoystickKnobTextureRegion != null) {
			if (JoystickKnobTextureRegion.getTexture().isLoadedToHardware()) {
				JoystickKnobTextureRegion.getTexture().unload();
				JoystickKnobTextureRegion = null;
			}
		}

		if (pauseHUDTextureRegion != null) {
			if (pauseHUDTextureRegion.getTexture().isLoadedToHardware()) {
				pauseHUDTextureRegion.getTexture().unload();
				pauseHUDTextureRegion = null;
			}

		}
		
		if (menuTextureRegion != null) {
			if (menuTextureRegion.getTexture().isLoadedToHardware()) {
				menuTextureRegion.getTexture().unload();
				menuTextureRegion = null;
			}

		}

		if (mUnit1HUDTextureRegion != null) {
			if (mUnit1HUDTextureRegion.getTexture().isLoadedToHardware()) {
				mUnit1HUDTextureRegion.getTexture().unload();
				mUnit1HUDTextureRegion = null;
			}
		}
		if (mUnit2HUDTextureRegion != null) {
			if (mUnit2HUDTextureRegion.getTexture().isLoadedToHardware()) {
				mUnit2HUDTextureRegion.getTexture().unload();
				mUnit2HUDTextureRegion = null;
			}
		}
		if (mUnit3HUDTextureRegion != null) {
			if (mUnit3HUDTextureRegion.getTexture().isLoadedToHardware()) {
				mUnit3HUDTextureRegion.getTexture().unload();
				mUnit3HUDTextureRegion = null;
			}
		}
		if (mUnit4HUDTextureRegion != null) {
			if (mUnit4HUDTextureRegion.getTexture().isLoadedToHardware()) {
				mUnit4HUDTextureRegion.getTexture().unload();
				mUnit4HUDTextureRegion = null;
			}
		}
		if (mUnit5HUDTextureRegion != null) {
			if (mUnit5HUDTextureRegion.getTexture().isLoadedToHardware()) {
				mUnit5HUDTextureRegion.getTexture().unload();
				mUnit5HUDTextureRegion = null;
			}
		}
		if (mUnit6HUDTextureRegion != null) {
			if (mUnit6HUDTextureRegion.getTexture().isLoadedToHardware()) {
				mUnit6HUDTextureRegion.getTexture().unload();
				mUnit6HUDTextureRegion = null;
			}
		}
	}

	// ============================ LOAD GAME TEXTURES ================= //
	public void loadGameTextures() {
		// Store the current asset base path to apply it after we've loaded our
		// textures
		mPreviousAssetBasePath = BitmapTextureAtlasTextureRegionFactory
				.getAssetBasePath();
		// Set our game assets folder to "assets/gfx/game/"
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");

		
		/* Create the hero1 texture atlas */
		BuildableBitmapTextureAtlas hero1Texture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 1800, 300, TextureOptions.BILINEAR);

		/* Create the hero1 texture region */
		mHero1TextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(hero1Texture, context,
						"Sprite1.png", 9, 3);

		/* Build and load the hero1 texture atlas */
		try {
			hero1Texture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			hero1Texture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		/* Create the unit1 texture atlas */
		BuildableBitmapTextureAtlas towerTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 100, 200, TextureOptions.BILINEAR);

		/* Create the unit1 texture region */
		towerTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(towerTexture, context,
						"tower.png", 1, 1);

		/* Build and load the unit1 texture atlas */
		try {
			towerTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			towerTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		/* Create the unit1 texture atlas */
		BuildableBitmapTextureAtlas unit1Texture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 301, 100, TextureOptions.BILINEAR);

		/* Create the unit1 texture region */
		mUnit1TextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(unit1Texture, context,
						"brolywalking.png", 6, 1);

		/* Build and load the unit1 texture atlas */
		try {
			unit1Texture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			unit1Texture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		/* Create the unit3 texture atlas */
		BuildableBitmapTextureAtlas unit3Texture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 927, 99, TextureOptions.BILINEAR);

		/* Create the unit3 texture region */
		mUnit3TextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(unit3Texture, context,
						"broly_attacke_komplett_mit_spin.PNG", 6, 1);

		/* Build and load the unit3 texture atlas */
		try {
			unit3Texture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			unit3Texture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
		/* Create the unit3 texture atlas */
		BuildableBitmapTextureAtlas unit1EnemyTexture = new BuildableBitmapTextureAtlas(
				engine.getTextureManager(), 301, 100, TextureOptions.BILINEAR);

		/* Create the unit1 texture region */
		mUnit1EnemyTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(unit1EnemyTexture, context,
						"brolywalking_enemy.png", 6, 1);

		/* Build and load the unit1 texture atlas */
		try {
			unit1EnemyTexture
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			unit1EnemyTexture.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

		// Revert the Asset Path.
		BitmapTextureAtlasTextureRegionFactory
				.setAssetBasePath(mPreviousAssetBasePath);
	}

	// ============================ UNLOAD GAME TEXTURES =============== //
	public void unloadGameTextures() {
		// background texture - only unload it if it is loaded:
		if (mUnit1TextureRegion != null) {
			if (mUnit1TextureRegion.getTexture().isLoadedToHardware()) {
				mUnit1TextureRegion.getTexture().unload();
				mUnit1TextureRegion = null;
			}
		}
	}

	// ============================ LOAD TEXTURES (MENU) ================= //

}